<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col
            :md="8"
            :sm="24"
          >
            <a-form-item label="登录账号">
              <a-input placeholder="" />
            </a-form-item>
          </a-col>
          <a-col
            :md="8"
            :sm="24"
          >
            <a-form-item label="账号状态">
              <a-select
                placeholder="请选择"
                default-value="0"
              >
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">正常</a-select-option>
                <a-select-option value="2">已注销</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">

            <a-col
              :md="8"
              :sm="24"
            >
              <a-form-item label="添加日期">
                <a-date-picker
                  style="width: 100%"
                  placeholder="请输入添加日期"
                />
              </a-form-item>
            </a-col>
            <a-col
              :md="8"
              :sm="24"
            >
              <a-form-item label="所任职务">
                <a-select
                  placeholder="请选择"
                  default-value="0"
                >
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">机构</a-select-option>
                  <a-select-option value="2">学校</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

          </template>
          <a-col
            :md="!advanced && 8 || 24"
            :sm="24"
          >
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
            >
              <a-button
                type="primary"
                @click="query"
              >查询</a-button>
              <a-button style="margin-left: 8px">重置</a-button>
              <a
                @click="toggleAdvanced"
                style="margin-left: 8px"
              >
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button
        type="primary"
        icon="plus"
        @click="add"
      >新建</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1">
          <a-icon type="delete" />删除</a-menu-item>
          <!-- lock | unlock -->
          <a-menu-item key="2">
          <a-icon type="lock" />锁定</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>

    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      @onSelect="onChange"
    >
      <span
        slot="action"
        slot-scope="text, record"
      >
        <a @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多
            <a-icon type="down" />
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;">详情</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">禁用</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">删除</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <a-modal
      title="新建预留用户"
      :width="800"
      v-model="visible1"
      @ok="handleOk"
      :confirmLoading="confirmLoading"
    >
      <a-form :autoFormCreate="(form)=>{this.form = form}">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='登录账号'
          hasFeedback
          validateStatus='success'
        >
          <a-input
            placeholder='loginName'
            v-model="mdl1.no"
            id='no'
          />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='手机号码'
          hasFeedback
          validateStatus='success'
          help='请填写手机号码'
        >
          <a-input-number
            :min="1"
            id="callNo"
            v-model="mdl1.callNo"
            style="width: 100%"
          />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='Email'
          hasFeedback
        >
          <a-input
            :rows="5"
            v-model="mdl1.description"
            placeholder="..."
            id='description'
          />
        </a-form-item>

      </a-form>
    </a-modal>

    <a-modal
      title="编辑用户"
      :width="800"
      v-model="visible"
      @ok="handleOk"
      :confirmLoading="confirmLoading"
    >
      <a-form :autoFormCreate="(form)=>{this.form = form}">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='登录账号'
          hasFeedback
          validateStatus='success'
        >
          <a-input
            placeholder='loginName'
            v-model="mdl.no"
            id='no'
          />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='手机号码'
          hasFeedback
          validateStatus='success'
          help='请填写手机号码'
        >
          <a-input-number
            :min="1"
            id="callNo"
            v-model="mdl.callNo"
            style="width: 100%"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='Email'
          hasFeedback
        >
          <a-input
            :rows="5"
            v-model="mdl.description"
            placeholder="..."
            id='description'
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label='任职'
          hasFeedback
        >
          <a-select
            mode="multiple"
            :defaultValue="['a1', 'b2']"
            style="width: 100%"
            @change="handleChange"
            placeholder="Please select"
          >
            <a-select-option
              v-for="i in 25"
              :key="(i + 9).toString(36) + i"
            >{{ '职务'+i }}</a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-modal>

  </a-card>
</template>

<script>
import STable from "@/components/table/";
import ATextarea from "ant-design-vue/es/input/TextArea";
import AInput from "ant-design-vue/es/input/Input";

import { getServiceList } from "@/api/manage";

export default {
  name: "TableList",
  components: {
    AInput,
    ATextarea,
    STable
  },
  data() {
    return {
      confirmLoading: false,
      visible1: false,
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 }
      },
      form: null,
      mdl: {},
      mdl1: {},

      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: "登录账号",
          dataIndex: "no"
        },
        {
          title: "email",
          dataIndex: "description"
        },
        {
          title: "手机号码",
          dataIndex: "callNo"
          // sorter: true,
          // needTotal: true,
          // customRender: (text) => text + ' 次'
        },
        {
          title: "更新时间",
          dataIndex: "updatedAt",
          sorter: true
        },
        {
          table: "操作",
          dataIndex: "action",
          width: "150px",
          scopedSlots: { customRender: "action" }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getServiceList(Object.assign(parameter, this.queryParam)).then(
          res => {
            return res.result;
          }
        );
      },

      selectedRowKeys: [],
      selectedRows: []
    };
  },
  created() {
    // getRoleList({ t: new Date()})
  },
  methods: {
    query() {
      const hide = this.$message.loading("查询中..", 0);
      setTimeout(hide, 2500);
    },
    add() {
      this.visible = true;
    },
    handleEdit(record) {
      this.mdl = Object.assign({}, record);
      console.log(this.mdl);
      this.visible = true;
    },
    handleOk() {
      this.confirmLoading = true;
      setTimeout(() => {
        this.visible1 = false;
        this.visible = false;
        this.confirmLoading = false;
        this.mdl1 = {};
        this.$message.success("操作成功");
      }, 2000);
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys;
      this.selectedRows = row.selectedRows;

      console.log(this.$refs.table);
    },
    toggleAdvanced() {
      this.advanced = !this.advanced;
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
};
</script>
<style>
.table-operator {
  margin-bottom: 10px;
}
</style>
